/**
 * @author htl
 * @date 2018/12/18 0018
 * @Description:
 */
const Promise = require("bluebird");
const request = Promise.promisify(require("request"));
const api = {
    http(url,method='post',data={},type) {
        const option = {
            url: url,
            method,
        }
        if(!type) {
            return api.apiByForm(option,data);
        }else {
            return api.apiByJson(option,data);
        }
    },
    apiByForm(opt,data) {
        const option = {
            ...opt,
            form: data
        }
        return request(option).then((response)=> {
            if(response.statusCode === 200) {
                return JSON.parse(response.body);
            }else {
                return {
                    code: 9999,
                    msg: '异常'
                }
            }
        });
    },
    apiByJson(opt,data) {
        const option = {
            ...opt,
            json: true,
            headers: {
                "content-type": "application/json",
            },
            body:data || {}
        }
        console.log(option)
        return request(option).then((response)=> {
            if(response.statusCode === 200) {
                return response.body;
            }else {
                return {
                    code: 9999,
                    msg: '异常'
                }
            }
        });
    },
}

module.exports = api;